<template>
  <div id="app-container" style="margin: 10px">

    <!--搜索input框 搜索按钮 重置按钮-->
    <el-form :inline="true" style="margin-bottom: -10px">
      <el-form-item label="商品名称">
        <el-input v-model="productDTO.title" size="small" placeholder="用输入商品名称"/>
      </el-form-item>
      <el-button type="primary" icon="el-icon-search" size="small" @click="searchPage">搜索</el-button>
      <el-button plain icon="el-icon-refresh" size="small" style="margin-left: 0" @click="resetForm">清空</el-button>
    </el-form>

    <!--新增、修改、删除、导出按钮-->
    <el-form :inline="true">
      <router-link to="/product/add">
        <el-button type="primary" plain icon="el-icon-plus" size="small">新增数据</el-button>
      </router-link>
      <el-button type="danger" plain icon="el-icon-delete" size="small" @click="handleDeletePart">批量删除</el-button>
      <el-button type="warning" plain icon="el-icon-download" size="small" style="margin-left: 0" @click="handleExport">导出数据</el-button>
    </el-form>

    <!--table表格-->
    <el-table
      ref="multipleTable"
      :data="productlist"
      tooltip-effect="dark"
      style="width: 100%"
      @selection-change="handleSelectionChange">
      <el-table-column
        type="selection"/>
      <el-table-column type="index" label="序号" width="100" align="center" prop="id">
        <template slot-scope="scope">
          {{ (currentPage - 1) * pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column
        prop="title"
        label="产品名称"
        width="150"
        align="center"/>
      <el-table-column
        prop="price"
        label="产品价格"
        width="100"
        align="center"/>
      <el-table-column
        align="center"
        prop="createTime"
        label="创建时间"
        width="200"
        show-overflow-tooltip/>
      <el-table-column
        align="center"
        prop="updateTime"
        label="更新时间"
        show-overflow-tooltip/>

      <!--操作列 修改、删除按钮-->
      <el-table-column fixed="right" width="150" label="操作">

        <template slot-scope="scope">
          <router-link :to="'/product/update/' +scope.row.id">
            <el-button
              type="text"
              icon="el-icon-edit"
              size="mini">编辑
            </el-button>
          </router-link>
          <el-button
            type="text"
            icon="el-icon-delete"
            size="mini"
            @click="handleDelete(scope.row.id)">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--分页-->
    <div class="block" >
      <el-pagination
        :page-sizes="[10, 20, 30, 40]"
        :page-size="pageSize"
        :current-page="currentPage"
        :total="total"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"/>
    </div>
  </div>
</template>

<script>
import fileDownload from 'js-file-download'
import axios from 'axios'
import product from '../../api/product'

export default {
  data() {
    return {
      productDTO: {},
      productlist: [],

      multipleSelection: [],

      currentPage: 1,
      pageSize: 10,
      total: null
    }
  },
  created() {
    this.searchPage()
  },
  methods: {
    /* 多选*/
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    /* 删除方法*/
    handleDelete(id) {
      this.$confirm('是否确定删除', '提示信息', { type: 'warning' }).then(() => {
        product.deleteById(id).then(res => {
          this.$message.success('删除数据成功')
          this.searchPage()
        })
      }).catch(() => {
        this.$message.info('取消删除操作')
      })
    },
    /* 批量删除方法*/
    handleDeletePart() {
      this.$confirm('是否确定删除', '提示信息', { type: 'warning' }).then(() => {
        const ids = this.multipleSelection.map(product => product.id)
        product.deletePart(ids).then(response => {
          this.$message.success('删除成功')
          this.multipleSelection = []
          this.searchPage()
        })
      }).catch(() => {
        this.$message.info('取消删除操作')
      })
    },
    /* 分页方法 每页容量*/
    handleSizeChange(size) {
      this.pageSize = size
      this.searchPage()
    },
    /* 分页方法 当前页*/
    handleCurrentChange(current) {
      this.currentPage = current
      this.searchPage()
    },
    /* 分页查询*/
    searchPage() {
      product.searchPage(this.currentPage, this.pageSize, this.productDTO).then(res => {
        this.productlist = res.data.data
        this.total = res.data.total
      })
    },
    /* 重置方法*/
    resetForm() {
      this.productDTO = {}
      this.searchPage()
    },
    handleExport() {
      axios({
        method: 'post',
        url: 'http://localhost:8080/product/export',
        responseType: 'blob',
        data: this.productDTO
      }).then(res => {
        if (res && res.data) {
          fileDownload(res.data, '产品信息.xlsx')
        }
      })
    }
  }
}
</script>
